<!-- 已废弃，移动至AMIS -->

<template>

  <ContentWrap>
    书签Key：<input type="text" v-model="bookmarksign"/>
    书签Name：<input type="text" v-model="bookmarkname"/>
    替换内容：<input type="text" v-model="bookmarkValue"/>
    <el-button type="primary" plain @click="insertBookmark()">
      <Icon icon="ep:plus" class="mr-5px" /> 插入书签
    </el-button>
    <el-button type="primary" plain @click="replaceBookmark()">
      <Icon icon="ep:plus" class="mr-5px" /> 替换书签
    </el-button>
  </ContentWrap>

</template>

<script lang="ts" setup>

defineOptions({name: 'InsertBookmark'})

const bookmarksign = ref('bookmark1');
const bookmarkname = ref("我是测试书签1");
const bookmarkValue = ref("书签替换内容");

const insertBookmark = async function () {
  // 插入文字型窗体域
  var wpsApp = wps.WpsApplication();
  var doc = wpsApp.ActiveDocument;
  var selection = wpsApp.ActiveWindow.Selection;
  var range = selection.Range;
  var fields = doc.FormFields;
  fields.Shaded = true; // 显示底纹

  var formField = fields.Add(range, wps.Enum&&wps.Enum.wdFieldFormTextInput||70);//枚举值兼容性写法，70为枚举表中wps.Enum.wdFieldFormTextInput的值
  formField.Name = bookmarksign.value; // 书签名称设置
  formField.Result = bookmarkname.value; // 窗体域的显示结果设置
  formField.bookmarkValue = bookmarkValue.bookmarkValue; // 书签值
  //选中，主动触发刷新
  wps.WpsApplication().ActiveDocument.Range(selection.Start,selection.Start).Select()
  // 添加成功给予提示
  alert("书签【" + formField.Name + "】添加成功！");
}

const replaceBookmark = async function () {
  // 插入文字型窗体域
  var wpsApp = wps.WpsApplication();
  var doc = wpsApp.ActiveDocument;
  let bookMarks = doc.Bookmarks
  let bookMarkItem = bookMarks.Item(bookmarksign.value)
  if (bookMarkItem) {
    let bookMarkName = bookMarkItem.Name
    let bookMarkRange = bookMarkItem.Range
    bookMarkRange.Text = bookmarkValue.value
    bookMarkRange.Select()  //内容替换后不会刷新，让wps作一次刷新，这可能是wps的bug
    //检测一下bookmark是不是失效了
    if (!bookMarks.Item(bookMarkName)){
      bookMarks.Add(bookMarkName, bookMarkRange)
    }
  }
}

</script>
